package com.duan.boot.service.impl;

import com.duan.boot.entity.Department;
import com.duan.boot.entity.User;
import com.duan.boot.mapper.DepMapper;
import com.duan.boot.mapper.HomePageMapper;
import com.duan.boot.service.DepService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author duan
 */
@Service
public class DepServiceImpl implements DepService{
    @Resource
    private DepMapper depMapper;

    @Resource
    private HomePageMapper homePageMapper;

    @Resource
    private UserServiceImpl userService;

    @Override
    public Department getDepById(int id) {
        if (findAllDepId().contains(id)){
            return getDepByName(depMapper.selectDepById(id));
        }else {
            return null;
        }
    }

    @Override
    public Department getDepByName(String name) {
        if (findAllDepName().contains(name)){
            Department department = depMapper.getDepByName(name);
            department.setAllUser(showUser(name));
            return department;
        }else {
            return null;
        }
    }

    @Override
    public Department getWithUser(User user) {
        if (userService.allUserName().contains(user.getUserName()) && userService.allUserId().contains(user.getUserId())){
            Department department = depMapper.getDepByName(userService.selectUserByName(user.getUserName()).getUserDepartment());
            department.setAllUser(showUser(userService.selectUserByName(user.getUserName()).getUserDepartment()));
            return department;
        }else {
            return null;
        }
    }

    @Override
    public boolean adduser(User user) {
        homePageMapper.insertUser(user);
        return depMapper.adduser(user);
    }

    @Override
    public List<User> showUser(String name) {
        return depMapper.showUser(name);
    }

    @Override
    public boolean removeUser(int id) {
        homePageMapper.removeUser(id);
        return depMapper.removeUser(id);
    }

    @Override
    public Integer saveUser(User user) {
        return depMapper.saveUser(user);
    }

    @Override
    public Map<String, Object> findAllUserOrderByName(String name, Integer pageIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        //查询学生
        List<User> userList = showUser(name);
        if (CollectionUtils.isEmpty(userList)){
            map.put("pageIndex", pageIndex.toString());
            map.put("pageSize", pageSize.toString());
            map.put("total", "0");
            map.put("pages", 0);
            map.put("list", new ArrayList<>());
            return map;
        }
        return map;
    }

    @Override
    public Department searchDep(Object idOrName) {
        try {
            if (findAllDepName().contains(idOrName.toString())){
                return getDepByName(idOrName.toString());
            }else if (findAllDepId().contains(Integer.parseInt(idOrName.toString()))){
                return getDepById(Integer.parseInt(idOrName.toString()));
            }else {
                return null;
            }
        }catch (NumberFormatException e){
            return null;
        }

    }

    @Override
    public List<String> findAllDepName() {
        List<String> list = new ArrayList<>();
        for (Department department : findAll()){
            list.add(department.getDepName());
        }
        return list;
    }

    @Override
    public List<Integer> findAllDepId() {
        List<Integer> list = new ArrayList<>();
        for (Department department : findAll()){
            list.add(department.getDepId());
        }
        return list;
    }

    @Override
    public Department findUserByIdOrName(Object idOrName) {
        List<User> list = new ArrayList<>();
        User user = userService.selectByIdOrName(idOrName);
        if (user==null){
            return null;
        }else {
            Department department = getDepByName(user.getUserDepartment());
            list.add(user);
            department.setAllUser(list);
            return department;
        }
    }

    @Override
    public List<Map<String, Object>> findDepAndUser() {

        List<Map<String, Object>> list = new ArrayList<>();
        for (String depName : findAllDepName()){
            Map<String, Object> map = new HashMap<>();
            map.put("depId", getDepByName(depName).getDepId());
            map.put("depName", depName);
            Map<String, Object> map1 = new HashMap<>();
            for (User user : showUser(depName)){
                map1.put("id", user.getUserId());
                map1.put("name", user.getUserName());
            }
            map.put("children", map1);
            list.add(map);
        }
        return list;
    }

    @Override
    public Integer numberOfDep(Integer depId) {
        return showUser(getDepById(depId).getDepName()).size();
    }

    @Override
    public List<User> pagedQuery(Integer page, Integer numberOfPage, Integer depId) {
        Map<Integer, List<User>> map = new HashMap<>();
        //查询总的页数
        int totalPage = 0;
        if (numberOfDep(depId)%numberOfPage==0){
            totalPage = numberOfDep(depId)/numberOfPage;
        }else {
            totalPage = numberOfDep(depId) / numberOfPage + 1;
        }
        //第一层循环就是每一页的下标
        for (int i = 0; i<totalPage; i++){
            //第二层循环就是获得当前页多有的元素
            List<User> list = new ArrayList<>();
            for (int j = i*numberOfPage;j<numberOfPage+i*numberOfPage; j++){
                if (j==showUser(getNameById(depId)).size()){
                    break;
                }
                list.add(showUser(getNameById(depId)).get(j));

            }
            map.put(i, list);
        }
        return map.get(page-1);
    }

    @Override
    public Integer pagedQueryPage(Integer numberOfPage, Integer depId) {
        int totalPage = 0;
        if (numberOfDep(depId)%numberOfPage==0){
            totalPage = numberOfDep(depId)/numberOfPage;
        }else {
            totalPage = numberOfDep(depId) / numberOfPage + 1;
        }
        return totalPage;
    }

    @Override
    public String getNameById(Integer depId) {
        return getDepById(depId).getDepName();
    }

    @Override
    public boolean upDate(Department department) {
        if (findAllDepName().contains(department.getDepName())){
            return depMapper.upDate(department);
        }else {
            return false;
        }

    }


    @Override
    public boolean insert(Department department) {
        return depMapper.insert(department);
    }

    @Override
    public boolean delete(int id) {
        if (findAllDepId().contains(id)){
            return depMapper.delete(id);
        }else {
            return false;
        }

    }

    @Override
    public List<Department> findAll() {
        List<Department> list = new ArrayList<>();
        for (Department department : depMapper.selectAll()){
            department.setAllUser(showUser(department.getDepName()));
            list.add(department);
        }
        return list;
    }

}
